home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / apache / NcgiRequest.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  4.8 KB  |  158 lines

  1. // NcgiRequest - read and handle an NCGI request
  2. //
  3. // Copyright (c) 1996 Sun Microsystems, Inc.  All Rights reserved
  4. // Permission to use, copy, modify, and distribute this software
  5. // and its documentation for NON-COMMERCIAL purposes and without
  6. // fee is hereby granted provided that this copyright notice
  7. // appears in all copies. Please refer to the file copyright.html
  8. // for further important copyright and licensing information.
  9. //
  10. // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11. // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14. // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  
  17. package sun.servlet.apache;
  18.  
  19. import java.io.*;
  20. import java.util.*;
  21.  
  22. /** Read and handle an NCGI request.
  23. ** <P>
  24. ** This handles the server side of the NCGI protocol.  Given an
  25. ** input stream, it reads the request, does error checking etc.,
  26. ** and then provides the data.
  27. ** <P>
  28. ** The NCGI protocol is fairly simple.  There are four parts:
  29. ** <OL>
  30. ** <LI>
  31. ** The first part is to send a "magic" string identifying the protocol
  32. ** and version.  That string is currently "NCGI/1.0".  The string is
  33. ** preceeded by the length as an unsigned short in network order.
  34. ** <LI>
  35. ** The second part is authorization.
  36. ** The server has previously written a random string to a designated file.
  37. ** The file is protected so that it can only be read by the userid running
  38. ** the CGI program.  That program, the client, reads the file and sends
  39. ** the entire contents to the server, again preceeded by the length as an
  40. ** unsigned short in network order.
  41. ** <LI>
  42. ** The third part is to send the request.
  43. ** Since CGI has already encapsulated the request as environment variables,
  44. ** the client just sends those strings.  Again each one is preceeded by
  45. ** its length as an unsigned short in network order.  A length of zero
  46. ** is sent at the end, indicating there are no more strings.
  47. ** <LI>
  48. ** The fourth part is to send any data back and forth.
  49. ** The client reads data from stdin and sends it to the server, while
  50. ** simultaneously the response data from the server is read and then
  51. ** written to stdout.  This continues until the client gets an EOF
  52. ** reading from the server.  At that point the transaction is complete,
  53. ** the socket is closed, and the client exits.
  54. ** </OL>
  55. */
  56.  
  57. public class NcgiRequest
  58.     {
  59.  
  60.     private static final String MAGIC = "NCGI/1.0";
  61.  
  62.     private Hashtable hash;
  63.  
  64.     /** Read the initial NCGI request data.
  65.     ** @param in the stream to read the request data from
  66.     ** @exception NcgiException if there's a problem
  67.     */
  68.     public NcgiRequest( InputStream in, NcgiServer ncgiServer ) throws NcgiException
  69.     {
  70.     hash = new Hashtable();
  71.     DataInputStream din = new DataInputStream( in );
  72.     try
  73.         {
  74.         // Read and check magic.
  75.         String magic = readString( din );
  76.         if ( ! magic.equals( MAGIC ) )
  77.         throw new NcgiException( "bad magic" );
  78.         // Read and check auth.
  79.         byte[] auth = readBlock( din );
  80.         if ( ! equalsBytes( auth, ncgiServer.authBytes ) )
  81.         throw new NcgiException( "bad auth" );
  82.         // Read environment variables.
  83.         while ( true )
  84.         {
  85.         String env = readString( din );
  86.         if ( env.length() == 0 )
  87.             break;
  88.         int eq = env.indexOf( '=' );
  89.         if ( eq != -1 )
  90.             {
  91.             String name = env.substring( 0, eq );
  92.             String val = env.substring( eq + 1 );
  93.             hash.put( name, val );
  94.             }
  95.         }
  96.         // That'll do it.
  97.         }
  98.         catch (EOFException eof) {}// this means its time to leave
  99.     catch ( IOException e )
  100.         {
  101.         throw new NcgiException(
  102.         "problem reading request: " + e.toString() );
  103.         }
  104.     }
  105.  
  106.  
  107.     /** Read a string, preceeded by its length as an unsigned short
  108.     ** in network order.
  109.     */
  110.     private String readString( DataInputStream din ) throws IOException
  111.     {
  112.     byte[] b = readBlock( din );
  113.     return new String( b, 0 );
  114.     }
  115.  
  116.  
  117.     /** Read a block of bytes, preceeded by its length as an unsigned short
  118.     ** in network order.
  119.     */
  120.     private byte[] readBlock( DataInputStream din ) throws IOException
  121.     {
  122.     int len = din.readUnsignedShort();
  123.     byte[] b = new byte[len];
  124.     din.readFully( b );
  125.     return b;
  126.     }
  127.     
  128.  
  129.     /** Utility routine to check two byte arrays for equality.
  130.     */
  131.     public static boolean equalsBytes( byte[] a, byte[] b )
  132.     {
  133.     if ( a.length != b.length )
  134.         return false;
  135.     for ( int i = 0; i < a.length; ++i )
  136.         if ( a[i] != b[i] )
  137.         return false;
  138.     return true;
  139.     }
  140.  
  141.  
  142.     /** Get the value of the specified NCGI variable, which is actually
  143.     ** just the identically-named CGI environment variable.
  144.     */
  145.     public String getVar( String name )
  146.     {
  147.     return (String) hash.get( name );
  148.     }
  149.  
  150.  
  151.     /**
  152.      * Get a list of all NCGI variables.
  153.      */
  154.     public Enumeration getVars() {
  155.     return hash.keys();
  156.     }
  157.     }
  158.